<template>
	<view class="page">
		<!-- 用户信息列表 -->
		<view class="user-list">
			<button class=" flex flex-align " @click="getupload" style="background: transparent;padding: 0;">
				<view class="list" style="height: 160rpx;">
					<view class="title">
						<text>头像</text>
					</view>
					<view class="more-content">
						<image :src="dataInfo.headimg" mode=""></image>
						<text class="iconfont icon-more more"></text>
					</view>
				</view>
			</button>
			
			<view class="list" @click="onNickname">
				<view class="title">
					<text>昵称</text>
				</view>
				<view class="more-content">
					<input type="nickname" v-model="nickname" placeholder="请输入微信昵称"  @blur="getName" style="text-align: right;">
					<!-- <text class="content">{{nickname}}</text> -->
					<text class="iconfont icon-more more"></text>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>性别</text>
				</view>
				<view class="more-content">
					<text class="content">{{sexText}}</text>
					<text class="iconfont icon-more more"></text>
				</view>
				<view class="picker">
					<picker @change="sexPickerChange" :value="sexIndex" :range="sexArray">
						<view class="uni-input" style="height: 100rpx;">{{sexText}}</view>
					</picker>
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>出生日期</text>
				</view>
				<view class="more-content">
					<text class="content">{{birthday}}</text>
					<text class="iconfont icon-more more"></text>
				</view>
				<view class="picker">
					<picker @change="birthdayPickerChange" mode="date" :value="birthdayDate" :start="startDate"
						:end="endDate">
						<view class="uni-input" style="height: 100rpx;">{{birthdayDate}}</view>
					</picker>
				</view>
			</view>
		</view>
		<!-- 提示框 -->
		<DialogBox ref="DialogBox"></DialogBox>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				// 性别
				sexArray: ['男', '女', '保密'],
				sexIndex: 0,
				sexText: '保密',
				// 生日
				birthdayDate: currentDate,
				startDate: this.getDate('start'),
				endDate: this.getDate('end'),
				birthday: '2020-02-02',
				DialogBox: {},
				// 昵称
				nickname: '',
				dataInfo:''
			};
		},
		onShow() {
			this.getdata()
		},
		methods: {
			getdata(){
				let that = this
				that.tui.request("api.auth.center/get", "POST", {}, false, false, true).then((res) => {
					if(res.code==1){
						that.dataInfo = res.data
						that.nickname = res.data.nickname
					}
				}).catch((res) => {
					
				})
			},
			getName(e){
				let that = this
				that.tui.request("api.auth.center/set", "POST", {nickname:e.detail.value}, false, false, true).then((red) => {
					that.tui.toast(red.info)
					if(info.code==1){
						that.getdata()
					}
				}).catch((res) => {
					
				})
			},
			getupload(e){
				let that = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: res => {
						// that.headerimg = res.tempFilePaths[0];
						let tempFilePaths = res.tempFilePaths[0];
						
						that.tui.uploadFile("api.data/upload",tempFilePaths).then((res) => {
							that.tui.request("api.auth.center/set", "POST",{headimg:res.data.url} , false, true, true).then((dd) => {
								that.tui.toast(dd.info)
								if(dd.code==1){
									setTimeout(()=>{
										 that.getdata()
									},1500)
								}
								
							}).catch((res) => {})
							
						}).catch((res) => {})
					}
				});
				
			},
			/**
			 * 性别
			 * @param {Object} e
			 */
			sexPickerChange(e) {
				this.sexIndex = e.detail.value;
				this.sexText = this.sexArray[this.sexIndex];
			},
			/**
			 * 生日
			 * @param {Object} e
			 */
			birthdayPickerChange(e) {
				this.birthday = e.detail.value;
			},
			/**
			 * 获取日期
			 * @param {Object} type
			 */
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			/**
			 * 昵称点击
			 */
			
		}
	}
</script>

<style scoped lang="scss">
	@import 'Information.scss';
</style>